package com.htc.android.footprints.view;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.htc.android.footprints.commonui.SwitchButton;
import com.htc.android.footprints.utils.FootprintUtils;
import com.htc.android.footprints.utils.StorageUtil;
import com.htc.android.footprints.utils.voice.Recorder;
import com.htc.android.footprints.vo.Footprint;

public class MediaControlPanelEditMode extends LinearLayout
{
  private static final String TAG = "MediaControlPanelEditMode";
  View.OnClickListener deleteListener = new View.OnClickListener()
  {
    public void onClick(View paramView)
    {
      try
      {
        if ((MediaControlPanelEditMode.this.mRecorder == null) || (MediaControlPanelEditMode.this.mRecorder.sampleFile() == null))
        {
          MediaControlPanelEditMode.this.showToast(MediaControlPanelEditMode.this.getResources().getString(2131361908));
          return;
        }
      }
      catch (Exception localException)
      {
        while (true)
        {
          localException.printStackTrace();
          MediaControlPanelEditMode.this.showToast(MediaControlPanelEditMode.this.getResources().getString(2131361908));
          continue;
          new HtcAlertDialog.Builder(MediaControlPanelEditMode.this.getContext()).setTitle(MediaControlPanelEditMode.this.getResources().getString(2131361957)).setIcon(17301543).setMessage(MediaControlPanelEditMode.this.getResources().getString(2131361907)).setPositiveButton(MediaControlPanelEditMode.this.getResources().getString(2131361893), new DialogInterface.OnClickListener()
          {
            public void onClick(DialogInterface paramDialogInterface, int paramInt)
            {
              paramDialogInterface.dismiss();
              MediaControlPanelEditMode.this.mRecorder.delete();
              if (MediaControlPanelEditMode.this.mFootprint != null)
              {
                MediaControlPanelEditMode.this.mFootprint.mAudioPath = null;
                MediaControlPanelEditMode.this.mFootprint.mAudioFileName = null;
                MediaControlPanelEditMode.this.mProgress.setProgress(0);
                MediaControlPanelEditMode.this.updateUI();
              }
            }
          }).setNegativeButton(MediaControlPanelEditMode.this.getResources().getString(2131361796), new DialogInterface.OnClickListener()
          {
            public void onClick(DialogInterface paramDialogInterface, int paramInt)
            {
              paramDialogInterface.dismiss();
            }
          }).show();
        }
      }
    }
  };
  public int mCurrentState;
  TextView mCurrentTime = null;
  ImageView mDeleteButton = null;
  Footprint mFootprint = null;
  final Handler mHandler = new Handler();
  SwitchButton mPlayButton = null;
  ProgressBar mProgress = null;
  SwitchButton mRecordButton = null;
  Recorder mRecorder = null;
  private long mRemainingTime = -1L;
  TextView mTotalTime = null;
  Runnable mUpdateTimer = new Runnable()
  {
    public void run()
    {
      MediaControlPanelEditMode.this.updateTimerView();
    }
  };
  Recorder.OnStateChangedListener mediaListener = new Recorder.OnStateChangedListener()
  {
    public void onError(int paramInt)
    {
      Resources localResources = MediaControlPanelEditMode.this.getResources();
      Object localObject = null;
      switch (paramInt)
      {
      default:
      case 1:
      case 2:
      }
      while (true)
      {
        if (localObject != null)
          new HtcAlertDialog.Builder(MediaControlPanelEditMode.this.getContext()).setTitle(2131361795).setIcon(17301543).setMessage((CharSequence)localObject).setPositiveButton(2131361893, null).setCancelable(false).show();
        return;
        localObject = localResources.getString(2131361891);
        continue;
        localObject = localResources.getString(2131361892);
      }
    }

    public void onStateChanged(int paramInt)
    {
      MediaControlPanelEditMode.this.mCurrentState = paramInt;
      MediaControlPanelEditMode.this.setScreenkeepOn(MediaControlPanelEditMode.this.mCurrentState);
      MediaControlPanelEditMode.this.updateUI();
    }
  };
  SwitchButton.OnCheckedChangeListener playbackListener = new SwitchButton.OnCheckedChangeListener()
  {
    public void onCheckedChanged(SwitchButton paramSwitchButton, boolean paramBoolean)
    {
      if (MediaControlPanelEditMode.this.mRecorder == null);
      while (true)
      {
        return;
        if (MediaControlPanelEditMode.this.mRecorder.sampleFile() == null)
        {
          MediaControlPanelEditMode.this.showToast(MediaControlPanelEditMode.this.getResources().getString(2131361908));
          MediaControlPanelEditMode.this.mPlayButton.setChecked(false);
          continue;
        }
        if (paramBoolean);
        try
        {
          MediaControlPanelEditMode.this.mRecorder.startPlayback();
          while (true)
          {
            MediaControlPanelEditMode.this.updateUI();
            break;
            MediaControlPanelEditMode.this.mRecorder.stopPlayback();
          }
        }
        catch (Exception localException)
        {
          while (true)
            localException.printStackTrace();
        }
      }
    }
  };
  SwitchButton.OnCheckedChangeListener recordListener = new SwitchButton.OnCheckedChangeListener()
  {
    public void onCheckedChanged(SwitchButton paramSwitchButton, boolean paramBoolean)
    {
      if (MediaControlPanelEditMode.this.mRecorder == null)
        MediaControlPanelEditMode.this.mRecordButton.setChecked(false);
      while (true)
      {
        return;
        if ((!StorageUtil.isExternalStorageEnough()) || (!StorageUtil.isInternalStorageEnough()))
        {
          MediaControlPanelEditMode.this.mRecordButton.setChecked(false);
          FootprintUtils.showToast(MediaControlPanelEditMode.this.getContext(), MediaControlPanelEditMode.this.getContext().getResources().getString(2131361887));
          continue;
        }
        if (paramBoolean)
        {
          MediaControlPanelEditMode.this.calRecordingTime();
          MediaControlPanelEditMode.this.mTotalTime.setText(MediaControlPanelEditMode.this.setupTimer(1000L * MediaControlPanelEditMode.this.mRemainingTime));
          MediaControlPanelEditMode.this.mRecorder.startRecording();
          continue;
        }
        MediaControlPanelEditMode.this.mRecorder.stopRecording();
      }
    }
  };

  public MediaControlPanelEditMode(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    initView();
  }

  private void calRecordingTime()
  {
    long l1 = StorageUtil.availableStorage();
    long l2;
    if (l1 > 0L)
    {
      l2 = l1 / 135L;
      if (l2 > 60L)
        this.mRemainingTime = 60L;
    }
    while (true)
    {
      return;
      this.mRemainingTime = l2;
      continue;
      this.mRemainingTime = -1L;
    }
  }

  private void setScreenkeepOn(int paramInt)
  {
    switch (paramInt)
    {
    default:
    case 0:
    case 2:
    case 3:
    case 1:
    }
    while (true)
    {
      return;
      ((Activity)getContext()).getWindow().clearFlags(128);
      continue;
      ((Activity)getContext()).getWindow().addFlags(128);
      continue;
      ((Activity)getContext()).getWindow().addFlags(128);
    }
  }

  private String setupTimer(long paramLong)
  {
    if (paramLong < 0L);
    Object[] arrayOfObject;
    for (String str = "--:--"; ; str = String.format("%02d:%02d", arrayOfObject))
    {
      return str;
      arrayOfObject = new Object[2];
      arrayOfObject[0] = Long.valueOf(paramLong / 1000L / 60L);
      arrayOfObject[1] = Long.valueOf(paramLong / 1000L % 60L);
    }
  }

  private void showToast(String paramString)
  {
    if (paramString != null)
      Toast.makeText(getContext(), paramString, 3000).show();
  }

  private void updateButtonControl(int paramInt)
  {
    switch (paramInt)
    {
    default:
      if (this.mRecorder != null)
        break;
    case 0:
    case 2:
    case 3:
    case 1:
    }
    while (true)
    {
      return;
      this.mRecordButton.setEnabled(true);
      this.mRecordButton.setChecked(false);
      this.mDeleteButton.setEnabled(true);
      this.mPlayButton.setEnabled(true);
      this.mPlayButton.setChecked(false);
      this.mProgress.setProgress(0);
      this.mCurrentTime.setText(setupTimer(0L));
      if (this.mRecorder == null)
        continue;
      if (this.mRecorder.sampleFile() != null)
      {
        this.mTotalTime.setText(setupTimer(this.mRecorder.mSampleLength));
        this.mTotalTime.setVisibility(0);
        this.mFootprint.mAudioPath = this.mRecorder.sampleFile().getAbsolutePath();
        this.mFootprint.mAudioFileName = this.mRecorder.sampleFile().getName();
        invalidate();
        continue;
      }
      this.mTotalTime.setText(setupTimer(-1L));
      this.mCurrentTime.setText(setupTimer(-1L));
      continue;
      this.mRecordButton.setEnabled(false);
      this.mDeleteButton.setEnabled(false);
      this.mPlayButton.setEnabled(true);
      if (this.mRecorder == null)
        continue;
      this.mCurrentTime.setText(setupTimer(this.mRecorder.progress()));
      this.mTotalTime.setText(setupTimer(this.mRecorder.mSampleLength));
      this.mTotalTime.setVisibility(0);
      continue;
      this.mRecordButton.setEnabled(false);
      this.mDeleteButton.setEnabled(false);
      this.mPlayButton.setEnabled(true);
      if (this.mRecorder == null)
        continue;
      this.mCurrentTime.setText(setupTimer(this.mRecorder.progress()));
      this.mTotalTime.setText(setupTimer(this.mRecorder.mSampleLength));
      this.mTotalTime.setVisibility(0);
      continue;
      this.mRecordButton.setEnabled(true);
      this.mDeleteButton.setEnabled(false);
      this.mPlayButton.setEnabled(false);
      if (this.mRecorder == null)
        continue;
      this.mCurrentTime.setText(setupTimer(this.mRecorder.progress()));
      this.mTotalTime.setVisibility(0);
      continue;
      this.mCurrentTime.setText(setupTimer(this.mRecorder.progress()));
      this.mTotalTime.setText(setupTimer(this.mRecorder.mSampleLength));
    }
  }

  private void updateTimerView()
  {
    getResources();
    int i;
    TextView localTextView;
    if ((this.mCurrentState == 1) || (this.mCurrentState == 2) || (this.mCurrentState == 3))
    {
      i = 1;
      if ((this.mCurrentState == 1) && (this.mRecorder != null))
      {
        this.mCurrentTime.setText(setupTimer(this.mRecorder.progress()));
        int m = this.mProgress.getMax();
        this.mProgress.setProgress((int)(m * this.mRecorder.progress() / this.mRemainingTime / 1000L));
        if (this.mRecorder.progress() > 1000L * this.mRemainingTime)
          this.mRecordButton.setChecked(false);
      }
      if (this.mCurrentState == 2)
      {
        this.mCurrentTime.setVisibility(0);
        if (this.mRecorder != null)
        {
          long l = this.mRecorder.progress();
          this.mCurrentTime.setText(setupTimer(this.mRecorder.progress()));
          if (this.mRecorder.mSampleLength > 0)
          {
            this.mTotalTime.setText(setupTimer(this.mRecorder.mSampleLength));
            this.mProgress.setProgress((int)(1000L * l / this.mRecorder.mSampleLength));
          }
        }
      }
      if (this.mCurrentState == 3)
      {
        int j = this.mCurrentTime.getVisibility();
        localTextView = this.mCurrentTime;
        if (j != 4)
          break label298;
      }
    }
    label298: for (int k = 0; ; k = 4)
    {
      localTextView.setVisibility(k);
      if (i != 0)
        this.mHandler.postDelayed(this.mUpdateTimer, 500L);
      return;
      i = 0;
      break;
    }
  }

  private void updateUI()
  {
    updateButtonControl(this.mCurrentState);
    updateTimerView();
  }

  void initView()
  {
    addView((ViewGroup)((LayoutInflater)getContext().getSystemService("layout_inflater")).inflate(2130903066, null));
    this.mDeleteButton = ((ImageView)findViewById(2131492998));
    this.mDeleteButton.setOnClickListener(this.deleteListener);
    this.mRecordButton = ((SwitchButton)findViewById(2131492992));
    this.mRecordButton.setOnCheckedChangeListener(this.recordListener);
    this.mPlayButton = ((SwitchButton)findViewById(2131492993));
    this.mPlayButton.setOnCheckedChangeListener(this.playbackListener);
    this.mPlayButton.setOnClickListener(null);
    this.mCurrentTime = ((TextView)findViewById(2131492995));
    this.mCurrentTime.setText(setupTimer(-1L));
    this.mTotalTime = ((TextView)findViewById(2131492996));
    this.mTotalTime.setText(setupTimer(-1L));
    this.mProgress = ((ProgressBar)findViewById(2131492997));
  }

  public void resetState()
  {
  }

  public void setFootprintData(Footprint paramFootprint)
  {
    if (paramFootprint == null)
      return;
    this.mFootprint = paramFootprint;
    if (this.mFootprint.mAudioPath == null);
    for (this.mRecorder = new Recorder(); ; this.mRecorder = new Recorder(this.mFootprint.mAudioPath))
    {
      this.mRecorder.setOnStateChangedListener(this.mediaListener);
      updateUI();
      break;
    }
  }

  public void stopMediaControlPanel()
  {
    if (this.mRecorder == null);
    while (true)
    {
      return;
      this.mRecorder.stop();
      if ((this.mFootprint != null) && (this.mRecorder.sampleFile() != null))
      {
        this.mFootprint.mAudioFileName = this.mRecorder.sampleFile().getName();
        this.mFootprint.mAudioPath = this.mRecorder.sampleFile().getAbsolutePath();
      }
      updateButtonControl(0);
    }
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.view.MediaControlPanelEditMode
 * JD-Core Version:    0.6.0
 */